package com.ruoyi.web.service.impl;

import java.util.List;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.web.domain.*;
import com.ruoyi.web.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.mapper.DrugMapper;
import com.ruoyi.common.core.text.Convert;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * 药品管理Service业务层处理
 * 
 * @author drx
 * @date 2025-05-13
 */
@Service
public class DrugServiceImpl implements IDrugService 
{
    @Autowired
    private DrugMapper drugMapper;

    @Autowired
    private IDrugOtherService drugOtherService;

    @Autowired
    private IDrugEnterpriseService drugEnterpriseService;

    @Autowired
    private IDrugDiseaseService drugDiseaseService;

    @Autowired
    private IDrugTargetService drugTargetService;

    /**
     * 查询药品管理
     * 
     * @param id 药品管理主键
     * @return 药品管理
     */
    @Override
    public Drug selectDrugById(Long id)
    {
        Drug drug = drugMapper.selectDrugById(id);
        //获取药品其他信息集合
        DrugOther drugOther = new DrugOther();
        drugOther.setDrugId(id);
        List<DrugOther> drugOtherList = drugOtherService.selectDrugOtherList(drugOther);
        drug.setDrugOtherList(drugOtherList);
        //获取药品企业关联集合
        DrugEnterprise drugEnterprise = new DrugEnterprise();
        drugEnterprise.setDrugId(id);
        List<DrugEnterprise> drugEnterpriseList = drugEnterpriseService.selectDrugEnterpriseList(drugEnterprise);
        drug.setDrugEnterpriseList(drugEnterpriseList);
        //获取药品疾病关联集合
        DrugDisease drugDisease = new DrugDisease();
        drugDisease.setDrugId(id);
        List<DrugDisease> drugDiseaseList = drugDiseaseService.selectDrugDiseaseList(drugDisease);
        drug.setDrugDiseaseList(drugDiseaseList);
        //获取药品靶点关联集合
        DrugTarget drugTarget = new DrugTarget();
        drugTarget.setDrugId(id);
        List<DrugTarget> drugTargetList = drugTargetService.selectDrugTargetList(drugTarget);
        drug.setDrugTargetList(drugTargetList);
        return drug;
    }

    /**
     * 查询药品管理列表
     * 
     * @param drug 药品管理
     * @return 药品管理
     */
    @Override
    public List<Drug> selectDrugList(Drug drug)
    {
        return drugMapper.selectDrugList(drug);
    }

    /**
     * 新增药品管理
     * 
     * @param drug 药品管理
     * @return 结果
     */
    @Override
    public int insertDrug(Drug drug)
    {
        int i = 0;
        if(null == drug.getId()){
            drug.setCreateTime(DateUtils.getNowDate());
            drug.setCreateBy(ShiroUtils.getSysUser().getLoginName());
            drug.setCreateName(ShiroUtils.getSysUser().getUserName());
            drug.setUpdateTime(DateUtils.getNowDate());
            drug.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
            drug.setUpdateName(ShiroUtils.getSysUser().getUserName());
            i = drugMapper.insertDrug(drug);
        }else{
            drug.setUpdateTime(DateUtils.getNowDate());
            drug.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
            drug.setUpdateName(ShiroUtils.getSysUser().getUserName());
            i = drugMapper.updateDrug(drug);
        }

        //保存其他信息表（tdk）
        if(null != drug.getDrugOtherList()){
            drug.getDrugOtherList().forEach(item -> {
                if(null != item.getId()){
                    drugOtherService.updateDrugOther(item);
                }else {
                    item.setDrugId(drug.getId());
                    drugOtherService.insertDrugOther(item);
                }
            });
        }
        //保存药品企业关联表
        if(null != drug.getDrugEnterpriseList()){
            drug.getDrugEnterpriseList().forEach(item -> {
                if(null != item.getId()){
                    drugEnterpriseService.updateDrugEnterprise(item);
                }else{
                    item.setDrugId(drug.getId());
                    drugEnterpriseService.insertDrugEnterprise(item);
                }
            });
        }
        //保存药品疾病关联表
        if(null != drug.getDrugDiseaseList()){
            drug.getDrugDiseaseList().forEach(item -> {
                if(null != item.getId()){
                    drugDiseaseService.updateDrugDisease(item);
                }else{
                    item.setDrugId(drug.getId());
                    drugDiseaseService.insertDrugDisease(item);
                }

            });
        }
        //保存药品靶点关联表
        if(null != drug.getDrugTargetList()){
            drug.getDrugTargetList().forEach(item -> {
                if(null != item.getId()){
                    drugTargetService.updateDrugTarget(item);
                }else{
                    item.setDrugId(drug.getId());
                    drugTargetService.insertDrugTarget(item);
                }
            });
        }
        return i;
    }

    /**
     * 修改药品管理
     * 
     * @param drug 药品管理
     * @return 结果
     */
    @Override
    public int updateDrug(Drug drug)
    {
        drug.setUpdateTime(DateUtils.getNowDate());
        drug.setUpdateBy(ShiroUtils.getSysUser().getLoginName());
        drug.setUpdateName(ShiroUtils.getSysUser().getUserName());
        return drugMapper.updateDrug(drug);
    }

    /**
     * 批量删除药品管理
     * 
     * @param ids 需要删除的药品管理主键
     * @return 结果
     */
    @Override
    public int deleteDrugByIds(String ids)
    {
        return drugMapper.deleteDrugByIds(Convert.toStrArray(ids),ShiroUtils.getSysUser().getLoginName());
    }

    /**
     * 删除药品管理信息
     * 
     * @param id 药品管理主键
     * @return 结果
     */
    @Override
    public int deleteDrugById(Long id)
    {
        return drugMapper.deleteDrugById(id,ShiroUtils.getSysUser().getLoginName());
    }
}
